package org.ssc.tickethouse.controller

import org.spockframework.spring.SpringBean
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.test.web.servlet.MockMvc
import org.ssc.tickethouse.entity.Order
import org.ssc.tickethouse.entity.Tickethouse
import org.ssc.tickethouse.repository.*
import spock.lang.Specification

import static io.restassured.module.mockmvc.RestAssuredMockMvc.given
import static org.hamcrest.core.IsEqual.equalTo

/**
 * Spock集成测试示例
 * 仅测试TickethouseHandler单个Controller，各项依赖用Mock模拟
 */
@WebMvcTest(controllers = TickethouseHandler.class)
class TickethouseHandlerInterfaceSpec extends Specification{

    @SpringBean
    TickethouseRepository tickethouseRepository = Mock(TickethouseRepository){
        getTicketByTid(1) >> new Tickethouse(tid: 1)
        updatestandby(1) >> 1
    }
    @SpringBean
    OrderRepository orderRepository = Mock(OrderRepository){
        insert(_) >> 1
    }
    @SpringBean
    AudienceRepository audienceRepository = Mock()
    @SpringBean
    CinemaRepository cinemaRepository = Mock()

    @Autowired
    private MockMvc mockMvc;

    def "Test buy ticket"(){
        when:"Call byTicket"
        given().
                mockMvc(mockMvc).
                param("tid", 1).
                param("aid", 2).
        when().
                get("/ticket/buyTicket").
        then().
                statusCode(200).
                body(equalTo("1"))
        then:"Verification of a mocked method"
            with(tickethouseRepository){
                1 * getTicketByTid(1) >> new Tickethouse(tid: 1)
                1 * updatestandby(1) >> 1
            }
            with(orderRepository){
                1 * insert(new Order(tid: 1, aid: 2,state: 1)) >> 1
            }
    }

}
